<?php

class Default_Models_Product extends Libs_Model {

    private $pro_id;
    private $name;
    private $thumb;
    private $cat_id;
    private $description;
    private $price;
    private $thumb_up;
    private $thumb_down;
    private $status;
    private $post_date;

    public function setProId($pro_id) {
        $this->pro_id = $pro_id;
    }

    public function getProId() {
        return $this->pro_id;
    }

    public function setName($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }

    public function setThumb($thumb) {
        $this->thumb = $thumb;
    }

    public function getThumb() {
        return $this->thumb;
    }

    public function setCatId($cat_id) {
        $this->cat_id = $cat_id;
    }

    public function getCatId() {
        return $this->cat_id;
    }

    public function setDescription($description) {
        $this->description = $description;
    }

    public function getDescription() {
        return $this->description;
    }

    public function setPrice($price) {
        $this->price = $price;
    }

    public function getPrice() {
        return $this->price;
    }

    public function setThumbUp($thumb_up) {
        $this->thumb_up = $thumb_up;
    }

    public function getThumbUp() {
        return $this->thumb_up;
    }

    public function setThumbDown($thumb_down) {
        $this->thumb_down = $thumb_down;
    }

    public function getThumbDown() {
        return $this->thumb_down;
    }

    public function setStatus($status) {
        $this->status = $status;
    }

    public function getStatus() {
        return $this->status;
    }

    public function setPostDate($post_date) {
        $this->post_date = $post_date;
    }

    public function getPostDate() {
        return $this->post_date;
    }

    public function __construct() {
        parent::__construct();
    }

    protected function setValue($row, $isKey = TRUE) {
        $pro = new Default_Models_Product();
        if ($isKey) {
            $pro->setProId($row['pro_id']);
        }
        $pro->setCatId($row['cat_id']);
        $pro->setDescription($row['description']);
        $pro->setName($row['name']);
        $pro->setPostDate($row['post_date']);
        $pro->setPrice($row['price']);
        $pro->setThumbUp($row['thumb_up']);
        $pro->setThumbDown($row['thumb_down']);
        $pro->setStatus($row['status']);
        $pro->setThumb($row['thumb']);
        return $pro;
    }

    protected function getKeyAndValue(Default_Models_Product $pro, $isKey = FALSE) {
        $data = array();
        if ($isKey) {
            $data['pro_id'] = $pro->getProId();
        }
        $data['name'] = $pro->getName();
        $data['thumb'] = $pro->getThumb();
        $data['post_date'] = $pro->getPostDate();
        $data['price'] = $pro->getPrice();
        $data['thumb_up'] = $pro->getThumbUp();
        $data['thumb_down'] = $pro->getThumbDown();
        $data['status'] = $pro->getStatus();
        $data['description'] = $pro->getDescription();
        $data['cat_id'] = $pro->getCatId();
        return $data;
    }

    public function getNewProducts($limit = FALSE) {
        if (isset($limit) && $limit != FALSE) {
            $sql = "select * from tbl_products order by post_date desc limit $limit";
        } else {
            $sql = "select * from tbl_products order by post_date desc";
        }
        $result = $this->db->fetchAllO($sql);
        while ($rows = mysql_fetch_assoc($result)) {
            $out[] = $this->setValue($rows);
        }
        return $out;
    }

    public function getProDetail($id) {
        $sql = "select * from tbl_products where pro_id = $id";
        $result = $this->db->fetchOneO($sql);
        if ($row = mysql_fetch_assoc($result)) {
            $out = $this->setValue($row);
        }
        return $out;
    }

    public function getRelatePro($cat_id, $pro_id) {
        $sql = "select * from tbl_products where cat_id = $cat_id and pro_id <> $pro_id order by name limit 6";
        $result = $this->db->fetchAllO($sql);
        while ($rows = mysql_fetch_assoc($result)) {
            $out[] = $this->setValue($rows);
        }
        return $out;
    }

    public function getProductsByKeyword($keyword) {
        $sql = "select * from tbl_products where name like '%$keyword%' order by post_date desc";
        $result = $this->db->fetchAllO($sql);
        while ($row = mysql_fetch_assoc($result)) {
            $out[] = $this->setValue($row);
        }
        return $out;
    }

    public function getProductsByCat($id, $limit = FALSE) {
        if (isset($limit) && $limit != FALSE) {
            $sql = "select a.* from tbl_products a join tbl_categories b on a.cat_id = b.cat_id where a.cat_id = $id or b.parent_id = $id order by a.post_date desc limit $limit";
        } else {
            $sql = "select a.* from tbl_products a join tbl_categories b on a.cat_id = b.cat_id where a.cat_id = $id or b.parent_id = $id order by a.post_date desc";
        }
        $result = $this->db->fetchAllO($sql);
        while ($rows = mysql_fetch_assoc($result)) {
            $out[] = $this->setValue($rows);
        }
        return $out;
    }

    public function getCatNameById($id) {
        $sql = "select name from tbl_categories where cat_id = $id";
        $row = $this->db->fetchOne($sql);
        return $row['name'];
    }

    public function getCatNameByProId($pro_id) {
        $sql = "select b.name from tbl_products a join tbl_categories b on a.cat_id = b.cat_id where a.pro_id = $pro_id";
        $row = $this->db->fetchOne($sql);
        return $row['name'];
    }

    public function getProductCart() {
        @session_start();
//        session_destroy();
        if (count($_SESSION['cart']) > 0) {
            foreach ($_SESSION['cart'] as $k => $v) {
                $list[] = $k;
            }
            $listId = implode(',', $list);

//            $listId = preg_replace('/,,/', ',', $listId);

            $sql = "select pro_id, name, price, thumb from tbl_products where pro_id in ($listId)";
            $result = $this->db->fetchAllO($sql);
            while ($rows = mysql_fetch_assoc($result)) {
                $out[] = $this->setValue($rows);
            }
            return $out;
        } else {
            return false;
        }
    }

    public function getSaleProducts() {
        $sql = "select a.* from tbl_products a join tbl_sales b on a.pro_id = b.pro_id where b.date_end > now() order by b.date_end";
        $result = $this->db->fetchAllO($sql);
        while ($rows = mysql_fetch_assoc($result)) {
            $out[] = $this->setValue($rows);
        }
        return $out;
//        return $this->db->fetchAll($sql);
    }

}
